package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVo;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author lsyandwh
 * @since 2022-12-02
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBind(UserBindVo userBindVo, String userId) {

        //1.保存用户绑定的信息
        //根据userId查询用户绑定信息
        QueryWrapper<UserBind> wrapper = new QueryWrapper<UserBind>()
                .eq("user_id",userId);
        UserBind userBind = baseMapper.selectOne(wrapper);
        if(null == userBind){

            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVo,userBind);
            userBind.setUserId(Long.parseLong(userId));
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            //保存用户绑定信息
            baseMapper.insert(userBind);
        }else {
            BeanUtils.copyProperties(userBindVo,userBind);
            //更新用户绑定信息
            baseMapper.updateById(userBind);
        }

        //2.调用汇付宝api,生成汇付宝表单
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId",userId);
        paramMap.put("idCard",userBindVo.getIdCard());
        paramMap.put("personalName",userBindVo.getName());
        paramMap.put("bankType",userBindVo.getBankType());
        paramMap.put("bankNo",userBindVo.getBankNo());
        paramMap.put("mobile",userBindVo.getMobile());
        paramMap.put("returnUrl",HfbConst.USERBIND_RETURN_URL);// 汇付宝页面点击回跳尚融宝路径
        paramMap.put("notifyUrl",HfbConst.USERBIND_NOTIFY_URL);// 汇付宝内部回调尚融宝接口地址
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        //发送请求
        String form = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);

        return form;
    }

    @Override
    public void notifyBind(Map<String, Object> map) {
        //获取绑定码
        String bindCode = (String) map.get("bindCode");
        //获取会员id
        String userId = (String) map.get("agentUserId");

        //1.更新user_bind表示用户绑定信息
        //查询用户绑定信息
        QueryWrapper<UserBind> wrapper = new QueryWrapper<UserBind>()
                .eq("user_id",userId);
        UserBind userBind = baseMapper.selectOne(wrapper);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        userBind.setBindCode(bindCode);
        //更新表信息
        baseMapper.updateById(userBind);

        //2.更新user_info表信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        userInfo.setBindCode(bindCode);
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setNickName(userBind.getName());
        //更新表信息
        userInfoMapper.updateById(userInfo);
    }
}
